#### FIGURE 1 ####
#### Authore: Rithika Kumar     ####
#### Using survey experiment data ####
#### Figure 1 (Main text) ####

### Load required packages
library(haven)
library(ggplot2)
library(dplyr)
library(arm)
library(modelsummary)
library(car)
library(grid)
library(gridExtra)
library(ggsignif)
library(foreign)

# Set working directory to the "JOP Replicatio files" folder on your computer

## Import dataset 
survey_exp <- read_dta("DATA FILES TO SHARE/survey_experiment_long.dta")
### Create a binary variable for when woman is ranked number 1
### 2 is the code ofr hte woman being ranked first in the underlying data  

survey_exp$rank1_woman <- ifelse(survey_exp$exp_rank1 == 2,1,0)

## I will run the regression to get the average likelihood of a woman being ranked
## numner 1 across all four scenarios represented in both panels

### PANEL A
# 1. When husband is absent

## exp_mig_dummy = 0 when the experimental condition says husband is absent i.e. migrant
m1<- lm(rank1_woman~1 , data = survey_exp[survey_exp$exp_mig_dummy == 0, ])
summary(m1)
# 0.52900

## exp_mig_dummy = 1 when the experimental condition says husband is present i.e. non-migrant 

## 2. When husband is present (i.e. migrant)
m2<- lm(rank1_woman~1 , data = survey_exp[survey_exp$exp_mig_dummy == 1, ])
summary(m2)
# 0.21154 

### Statistical difference between the two models 
m3 <- lm(rank1_woman~exp_mig_dummy + mig_dummy, data = survey_exp)
summary(m3)
## We see that the difference between the two estimates remains statistically 
# significant even when we include a variable to control for the repsondent's own husband's migraiton status (mig_dummy)
## NOTE this is p<0.001

### PANEL B

## Average likelihood in each of the three conditions represented in panel B 

# 1. All gatekeepers present 
# when exp_mig_dummy = 1, exp_in_laws >0 and exp_child = 1 (all three gatekeepers are present)
m4<- lm(rank1_woman~1 , data = survey_exp[survey_exp$exp_mig_dummy == 1 & survey_exp$exp_in_laws >0 & survey_exp$exp_child == 1, ])
summary(m3)
# 0.20000

# 2. Only husband is present 
m5<- lm(rank1_woman~1 , data = survey_exp[survey_exp$exp_mig_dummy == 1 & survey_exp$exp_in_laws ==0 & survey_exp$exp_child == 0, ])
summary(m4)
## 0.28125 

## 3. ALL present except husband 
m6<- lm(rank1_woman~1 , data = survey_exp[survey_exp$exp_mig_dummy == 0 & survey_exp$exp_in_laws >0 & survey_exp$exp_child == 1, ])
summary(m5)
##  0.35928 

# Let us now get the significance 
## I am now subsetting each condition 

## When all gatekeepers are present 
survey_exp_subset_all <- survey_exp[survey_exp$exp_mig_dummy == 1 & survey_exp$exp_in_laws > 0 & survey_exp$exp_child == 1, ]
## Adding a new variable "all" with the value 1 to indicate that these are all responses when the all gatekeepers restrictionw as presented
survey_exp_subset_all$all <- 1
## Adding a new variable "with the "husb" which takes the value 1 
survey_exp_subset_all$husb <- 1

### ONLY husband is present 
survey_exp_subset_onlyhusb <- survey_exp[survey_exp$exp_mig_dummy ==1 & survey_exp$exp_in_laws ==0 & survey_exp$exp_child == 0, ]
survey_exp_subset_onlyhusb$all <- 0
survey_exp_subset_onlyhusb$husb<- 1

# now let us combine the two datasets because we need to find the difference 
## in average likelihood between the two conditions
survey_exp_all_onlyhus <- rbind(survey_exp_subset_all,survey_exp_subset_onlyhusb)

## get the significance 
m7 <- lm(rank1_woman~all + mig_dummy, data =survey_exp_all_onlyhus)
summary(m6)
## p =  0.202 

## Husband is not present but others are present 
survey_exp_subset_nohusb <- survey_exp[survey_exp$exp_mig_dummy ==0 & survey_exp$exp_in_laws >0 & survey_exp$exp_child == 1, ]
survey_exp_subset_nohusb$all <- 0
survey_exp_subset_nohusb$husb<- 0

## Now we will combine the condition where ALL were present and this new subset 

survey_exp_all_nohus <- rbind(survey_exp_subset_all,survey_exp_subset_nohusb)

m8 <- lm(rank1_woman~all + mig_dummy, data =survey_exp_all_nohus)
summary(m7)
## the difference is significant at -<0.001


## Time to make the graph! 

m1plot<- modelplot(m1, draw = F)
m2plot <- modelplot(m2, draw = F)
m4plot <- modelplot(m4, draw = F)
m5plot <- modelplot(m5, draw = F)
m6plot <- modelplot(m6, draw = F)



mplot <- rbind(m1plot,m2plot,m4plot,m5plot,m6plot)
mplot$Restriction <- c("Husband is absent", "Husband is present", "All", "Only husband", "All but husband") 
mplot$Category <- c("husband", "husband", "other_gate",
                    "other_gate", "other_gate") 



p<- grid.arrange(
  ggplot(mplot[mplot$Category== "husband",],aes(x=reorder(Restriction, estimate), y=estimate)) +
    geom_point(color="red",position = position_dodge(width=1/2))+
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2, color = "blue")+
    geom_signif(y_position = c(0.1), xmin = c(1) ,
                xmax = c(2), tip_length = 1, annotation = c("p<0.001"))+
    labs(
      x="Status of husband's presence in the household", y = "",
      title="")+
    ylim(0,0.6)+
    
    scale_linetype_manual(values=c("twodash"))+theme_bw()+theme(text = element_text(size = 13)),
  
  ggplot(mplot[mplot$Category== "other_gate",],aes(x=reorder(Restriction, estimate), y=estimate)) +
    geom_point(color="red",position = position_dodge(width=1/2))+
    geom_errorbar(aes(ymin = conf.low, ymax = conf.high), width = 0.2, color = "blue")+
    geom_signif(y_position = c(0.1, 0.05), xmin = c(1,1) ,
                xmax = c(2,3), tip_length = 1, annotation = c("p=0.20","p<0.001"))+
    labs(
      x="Male gatekeepers currently present in household", y = "",
      title="")+
    ylim(0,0.45)+
    
    scale_linetype_manual(values=c("twodash"))+theme_bw()+theme(text = element_text(size = 13)),ncol=2,
  
  top = textGrob("Likelihood of a woman being picked to carry out a political task (over other members)",
  ))
p
ggsave("OUTPUT/GRAPHS/Figure1.png", plot = p, width = 10, height = 6, dpi = 150, units = "in")
